<?php

//UMA new
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Carbon\Carbon;

class CheckTimeDiff implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        $range = [
            'am' => '12',
            'pm' => '17',
        ];
        $value = $range[$value];
        //如果是当天预约联系, 需要至少比截止时间提前2小时 比如上午的截止时间是12点,只能在上午10点前选择 “上午”,超过了
        // 10点, 就不让选择“上午”了;同理,如果下午的截止时间是 17:00,则只能在15点之前选择“下午”.
        $latestHour = $value - 2;
        $minute = 0;
        $second = 0;
        $latestTime = Carbon::createFromTimeString("$latestHour:$minute:$second");
        return now() < $latestTime;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'pls select a more later contact time';
    }
}
